package com.wxLogin.controller;

import com.alibaba.fastjson.JSONObject;

import com.commonmail.utils.RedisUtils;
import com.commonmail.utils.TokenUtils;
import com.wxLogin.config.WxLoginConfig;
import com.wxLogin.util.UrlUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class WxLoginController {
    @Resource
    private WxLoginConfig wxLoginConfig;
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private TokenUtils tokenUtils;

    /**
     * 显示微信登陆扫码界面
     *
     * @param response
     * @throws Exception
     */
    @RequestMapping("/wechat/login")
    public void wechat(HttpServletResponse response) throws Exception {
        // 构建获取二维码的URL
        StringBuffer wxsb = new StringBuffer(wxLoginConfig.getOpenUrl());
        wxsb.append("?appid=" + wxLoginConfig.getAppid());
        wxsb.append("&redirect_uri=" + wxLoginConfig.getRedirectUri());
        wxsb.append("&response_type=" + wxLoginConfig.getResponseType());
        wxsb.append("&scope=" + wxLoginConfig.getScope());
        wxsb.append("&state=" + wxLoginConfig.getState());
        response.sendRedirect(wxsb.toString());
    }

    /**
     * 用户手机确认后回调函数
     *
     * @param code
     * @throws Exception
     */
    @RequestMapping("/wechat/callback")
    public Object callback(String code) throws Exception {
        Map<String, Object> dto = new HashMap<>();
        // 构造请求URL
        StringBuffer wxsb = new StringBuffer(wxLoginConfig.getAccessTokenUrl());
        wxsb.append("?appid=" + wxLoginConfig.getAppid());
        wxsb.append("&secret=" + wxLoginConfig.getSecret());
        wxsb.append("&code=" + code);
        wxsb.append("&grant_type=" + wxLoginConfig.getGrantType());
        // 发送请求并获取accessToken和opendId
        String resp = UrlUtils.loadURL(wxsb.toString());
        JSONObject jsonObject = JSONObject.parseObject(resp);
        String accessToken = jsonObject.getString("access_token");
        String openId = jsonObject.getString("openid");
        // 构造获取用户信息的URL
        StringBuffer usb = new StringBuffer(wxLoginConfig.getUserInfoUrl());
        usb.append("?access_token=" + accessToken);
        usb.append("&openid=" + openId);
        // 发送请求并获取用户信息
        String userInfo = UrlUtils.loadURL(usb.toString());
        JSONObject userObject = JSONObject.parseObject(userInfo);
        if (userObject == null) {
            dto.put("msg", "微信登陆失败");
            dto.put("code", -4);
            return dto;
        }
        //生产token
        String token = tokenUtils.getToken("WX-");
    //    redisUtils.set(token, JSONObject.toJSONString(userObject), 30 * 60, TimeUnit.SECONDS);
        dto.put("msg", "微信登陆成功");
        dto.put("code", 4);
        dto.put("token", token);
        return dto;
    }
}

